package com.yzt.plugin_widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;

import androidx.appcompat.widget.AppCompatImageView;

/**
 * author : bob
 * date   : 2022/11/30
 * desc   :
 */
public class RoundImageView extends AppCompatImageView {
    /**
     * 圆形模式
     */
    private static final int MODE_CIRCLE = 1;
    /**
     * 普通模式
     */
    private static final int MODE_NONE = 0;
    /**
     * 圆角模式
     */
    private static final int MODE_ROUND = 2;
    private Paint mPaint;
    private int currMode = MODE_ROUND;
    /**
     * 圆角半径
     */
    private int currRound = dp2px(10);

    private int borderColor;

    private int borderWidth;

    public RoundImageView(Context context) {
        super(context);
        initViews();
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        obtainStyledAttrs(context, attrs, defStyleAttr);
        initViews();
    }

    private Path path;
    private void obtainStyledAttrs(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView, defStyleAttr, 0);
        currMode = a.hasValue(R.styleable.RoundImageView_type) ? a.getInt(R.styleable.RoundImageView_type, MODE_NONE) : MODE_NONE;
        currRound = a.hasValue(R.styleable.RoundImageView_round_radius) ? a.getDimensionPixelSize(R.styleable.RoundImageView_round_radius, currRound) : currRound;
        borderColor = a.getColor(R.styleable.RoundImageView_border_color, Color.TRANSPARENT);
        borderWidth = a.getDimensionPixelSize(R.styleable.RoundImageView_border_width, 0);
        a.recycle();
    }

    private void initViews() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(borderColor);
        mPaint.setStrokeWidth(borderWidth);
        path = new Path();
    }


    public void setCurrModeNone() {
        this.currMode = MODE_NONE;
    }

    public void setCurrModeCircle() {
        this.currMode = MODE_CIRCLE;
    }

    public void setRadius(int radius){
        this.currRound = dp2px(radius);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        if (currMode != MODE_NONE) {
//            path.reset();
            path.rewind();//性能更优
            if (currMode == MODE_CIRCLE) {//当为圆形模式的时候
                path.addRoundRect(new RectF(0, 0, getWidth(), getHeight()), getWidth() / 2, getHeight() / 2, Path.Direction.CW);
            } else {
                path.addRoundRect(new RectF(0, 0, getWidth(), getHeight()), currRound, currRound, Path.Direction.CW);
            }
            canvas.clipPath(path);
        }
        super.onDraw(canvas);

        if (borderWidth > 0) {
            if (currMode == MODE_CIRCLE) {//当为圆形模式的时候
                canvas.drawRoundRect((float) borderWidth / 2f, (float) borderWidth / 2f, getWidth() - borderWidth / 2f, getHeight() - borderWidth / 2f, getWidth() / 2f - borderWidth, getHeight() / 2f - borderWidth, mPaint);
            } else {
                canvas.drawRoundRect((float) borderWidth / 2f, (float) borderWidth / 2f, getWidth() - borderWidth / 2f, getHeight() - borderWidth / 2f, currRound, currRound, mPaint);
            }
        }
    }

    private int dp2px(float value) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getResources().getDisplayMetrics());
    }
}

